//
//  XMOrderMeetingView.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/6/21.
//

#import "XMOrderMeetingView.h"
#import "XMConfirmOrderMeetCell.h"

@interface XMOrderMeetingView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tbl;
@property (nonatomic, strong) UILabel *addressLabel;
@property (nonatomic, strong) UILabel *phoneLabel;

@property (nonatomic, strong) UIImageView *avatarImgView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) XMConfirmOrderMeetCell *meetInfoView; //见面包信息
@property (nonatomic, strong) UILabel *payPriceLabel;    //实付金额

@property (nonatomic, strong) NSArray *detailDataArr;
@property (nonatomic, strong) XMOrderDetailModel *orderDetailModel;

@property (nonatomic, strong) NSArray *titleArr;

@end

@implementation XMOrderMeetingView

//0 见面中。1 见面结束
- (instancetype)initWithFrame:(CGRect)frame meetingType:(NSString *)meetingType {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUIWithMeetingType:meetingType];
    }
    return self;
}

//0 见面中。 1 见面结束
- (void)creatUIWithMeetingType:(NSString *)meetType {
    if ([meetType isEqualToString:@"0"]) {
        _titleArr = @[@"支付方式：", @"下单时间：", @"付款时间：", @"见面实际开始时间："];
        self.navTitleLabel.text = @"见面中";
    }
    else {
        _titleArr = @[@"支付方式：", @"下单时间：", @"付款时间：", @"见面实际开始时间：", @"见面实际结束时间："];
        self.navTitleLabel.text = @"见面已结束";
    }
    [self addSubview:self.navView];
    
    self.navView.backgroundColor = kColorHEX(0xF6F6F6);
    self.backgroundColor = kColorHEX(0xF6F6F6);
    
    [self addSubview:self.tbl];
    
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(550.0))];
    headView.backgroundColor = kColorHEX(0xF6F6F6);
    //二维码
    UIView *qrcodeBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(332.0))];
    qrcodeBgView.backgroundColor = kColorHEX(0xffffff);
    [headView addSubview:qrcodeBgView];
    {
        UIImageView *qrcodeImgView = [[UIImageView alloc] initWithFrame:CGRectMake((self.width - KScaleWidth(256.0))/2, KScaleWidth(23.0), KScaleWidth(256.0), KScaleWidth(285.0))];
        qrcodeImgView.image = ImageNamed(@"order_detail_qrcode_used_img");
        [qrcodeBgView addSubview:qrcodeImgView];
        
        UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, KScaleWidth(8.0), qrcodeImgView.width, KScaleWidth(25.0)) font:KBoldFontSize(18.0) textColor:kColorHEX(0xffffff) textAlignment:NSTextAlignmentCenter];
        titleLabel.text = @"见面码";
        [qrcodeImgView addSubview:titleLabel];
        UILabel *subTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, titleLabel.maxY, titleLabel.width, KScaleWidth(20.0)) font:KFontSize(14.0) textColor:kColorHEX(0xffffff) textAlignment:NSTextAlignmentCenter];
        subTitleLabel.text = @"对方扫码核销后见面开始";
        [qrcodeImgView addSubview:subTitleLabel];
        UIImageView *addressIcon = [[UIImageView alloc] initWithFrame:CGRectMake(KScaleWidth(10.0), qrcodeImgView.height - KScaleWidth(44.0) - KScaleWidth(18.0), KScaleWidth(12.0), KScaleWidth(18.0))];
        addressIcon.contentMode = UIViewContentModeScaleAspectFit;
        addressIcon.image = ImageNamed(@"order_detail_meeting_address_icon");
        [qrcodeImgView addSubview:addressIcon];
        [qrcodeImgView addSubview:self.addressLabel];
        _addressLabel.y = qrcodeImgView.height - KScaleWidth(44.0) - KScaleWidth(18.0);
        [qrcodeImgView addSubview:self.phoneLabel];
        _phoneLabel.y = _addressLabel.maxY + KScaleWidth(3.0);
    }
    //个人信息
    UIView *userInfoBgView = [[UIView alloc] initWithFrame:CGRectMake(0, qrcodeBgView.maxY + KScaleWidth(8.0), self.width, KScaleWidth(204.0))];
    userInfoBgView.backgroundColor = kColorHEX(0xffffff);
    [headView addSubview:userInfoBgView];
    {
        [userInfoBgView addSubview:self.avatarImgView];
        [userInfoBgView addSubview:self.nameLabel];
        UIView *meetInfoBgView = [[UIView alloc] initWithFrame:CGRectMake(0, KScaleWidth(58.0), self.width, KScaleWidth(100.0))];
        [userInfoBgView addSubview:meetInfoBgView];
        [meetInfoBgView addSubview:self.meetInfoView];
        [userInfoBgView addSubview:self.payPriceLabel];
    }
    
    UIView *footView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(86.0))];
    footView.backgroundColor = kColorHEX(0xF6F6F6);
    if ([meetType isEqualToString:@"0"]) {
        UIButton *contactBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KBoldFontSize(14.0) frame:CGRectMake(footView.width - KScaleWidth(16.0) - KScaleWidth(90.0), KScaleWidth(16.0), KScaleWidth(90.0), KScaleWidth(38.0)) title:@"联系对方" titleColor:kColorHEX(0x333333)];
        [contactBtn setGradientLayerWithLocations:@[@(0), @(1.0)] colors:@[(__bridge id)kColorHEX(0xFFD800).CGColor, (__bridge id)kColorHEX(0xFFE44E).CGColor] startPoint:CGPointMake(0.5, 0.37) endPoint:CGPointMake(0.03, 0.37)];
        [contactBtn setCornerRadius:contactBtn.height/2];
        [contactBtn addTarget:self action:@selector(contactAction:) forControlEvents:UIControlEventTouchUpInside];
        [footView addSubview:contactBtn];
        
        UIButton *overBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(15.0) frame:CGRectMake(footView.width - KScaleWidth(113.0) - KScaleWidth(90.0), KScaleWidth(16.0), KScaleWidth(90.0), KScaleWidth(38.0)) title:@"确认结束" titleColor:kColorHEX(0x333333)];
        [overBtn setRadius:overBtn.height/2 borderColor:kColorHEX(0x979797) borderWidth:1];
        [overBtn addTarget:self action:@selector(overAction:) forControlEvents:UIControlEventTouchUpInside];
        [footView addSubview:overBtn];
    }
    else {
        UIButton *deleteBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(15.0) frame:CGRectMake(footView.width - KScaleWidth(113.0) - KScaleWidth(90.0), KScaleWidth(16.0), KScaleWidth(90.0), KScaleWidth(38.0)) title:@"删除订单" titleColor:kColorHEX(0x333333)];
        [deleteBtn setRadius:deleteBtn.height/2 borderColor:kColorHEX(0x979797) borderWidth:1];
        [deleteBtn addTarget:self action:@selector(deleteAction:) forControlEvents:UIControlEventTouchUpInside];
        [footView addSubview:deleteBtn];
        
        UIButton *evaluateBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(15.0) frame:CGRectMake(footView.width - KScaleWidth(16.0) - KScaleWidth(90.0), deleteBtn.y, KScaleWidth(90.0), KScaleWidth(38.0)) title:@"见面评价" titleColor:kColorHEX(0x333333)];
        [evaluateBtn setRadius:evaluateBtn.height/2 borderColor:kColorHEX(0x979797) borderWidth:1];
        [evaluateBtn addTarget:self action:@selector(evaluateAction:) forControlEvents:UIControlEventTouchUpInside];
        [footView addSubview:evaluateBtn];
    }
    
    _tbl.tableHeaderView = headView;
    _tbl.tableFooterView = footView;
}

- (void)setContentWithOrderDetailModel:(XMOrderDetailModel *)orderDetailModel {
    _orderDetailModel = orderDetailModel;
    _addressLabel.text = [NSString stringWithFormat:@"见面位置：%@", orderDetailModel.address];
    _phoneLabel.text = [NSString stringWithFormat:@"%@ %@", orderDetailModel.nickname, orderDetailModel.phone];
    
    [_avatarImgView sd_setImageWithURL:[NSURL URLWithString:orderDetailModel.avatar] placeholderImage:ImageNamed(@"mine_avatar_icon")];
    _nameLabel.text = orderDetailModel.nickname;
    [_meetInfoView setContentWithOrderDetailModel:orderDetailModel];
    NSString *payPriceStr = [NSString stringWithFormat:@"实付款 ¥%@", orderDetailModel.yinfuPrice];
    NSMutableAttributedString *payPriceAttr = [[NSMutableAttributedString alloc] initWithString:payPriceStr attributes:@{NSFontAttributeName : KBoldFontSize(18.0), NSForegroundColorAttributeName : kColorHEX(0xDD4B39)}];
    [payPriceAttr addAttributes:@{NSFontAttributeName : KFontSize(15.0), NSForegroundColorAttributeName : kColorHEX(0x666666)} range:NSMakeRange(0, 3)];
    [self.payPriceLabel setAttributedText:payPriceAttr];
    if (!orderDetailModel.startSeeTime) {
        orderDetailModel.startSeeTime = @"";
    }
    if (!orderDetailModel.payTime) {
        orderDetailModel.payTime = @"";
    }
    if (!orderDetailModel.orderTime) {
        orderDetailModel.orderTime = @"";
    }
    if (!orderDetailModel.endSeeTime) {
        orderDetailModel.endSeeTime = @"";
    }
    NSString *orderTime = [[NSDate dateWithTimeIntervalSince1970:[orderDetailModel.orderTime integerValue]/1000] dateStringWithFormat:@"yyyy-MM-dd HH:mm"];
    NSString *payTime = [[NSDate dateWithTimeIntervalSince1970:[orderDetailModel.payTime integerValue]/1000] dateStringWithFormat:@"yyyy-MM-dd HH:mm"];
    NSString *getTime = [[NSDate dateWithTimeIntervalSince1970:[orderDetailModel.startSeeTime integerValue]/1000] dateStringWithFormat:@"yyyy-MM-dd HH:mm"];
    NSString *overTime = [[NSDate dateWithTimeIntervalSince1970:[orderDetailModel.endSeeTime integerValue]/1000] dateStringWithFormat:@"yyyy-MM-dd HH:mm"];
    self.detailDataArr = @[@"微信支付", orderTime, payTime, getTime, overTime];
    [_tbl reloadData];
//    self.detailDataArr = @[orderDetailModel.];
}

//复制
- (void)copyAction:(UIButton *)button {
    UIPasteboard *pab = [UIPasteboard generalPasteboard];
    pab.string = _orderDetailModel.orderNo;
    if ([pab.string isEqualToString:_orderDetailModel.orderNo]) {
        [MBProgressHUD showSuccess:@"已复制到剪切板"];
    }
}

//联系对方
- (void)contactAction:(UIButton *)button {
    
}

//结束见面
- (void)overAction:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(meetOverWithOrderNo:)]) {
        [self.delegate meetOverWithOrderNo:_orderDetailModel.orderNo];
    }
}

//删除见面
- (void)deleteAction:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(meetDeleteAction)]) {
        [self.delegate meetDeleteAction];
    }
}

//评价
- (void)evaluateAction:(UIButton *)button {
    if (self.delegate && [self.delegate respondsToSelector:@selector(meetEvaluateAction)]) {
        [self.delegate meetEvaluateAction];
    }
}

#pragma mark - Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _titleArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return KScaleWidth(45.0);
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(45.0))];
    headView.backgroundColor = kColorHEX(0xffffff);
    
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), KScaleWidth(20.0), KScaleWidth(85.0), KScaleWidth(21.0)) font:KFontSize(15.0) textColor:kColorHEX(0x666666)];
    titleLabel.text = @"订单编号";
    [headView addSubview:titleLabel];
    UILabel *orderNoLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(100.0), titleLabel.y, self.width - KScaleWidth(100.0) - KScaleWidth(82.0), titleLabel.height) font:KFontSize(15.0) textColor:kColorHEX(0x666666) textAlignment:NSTextAlignmentRight];
    orderNoLabel.text = self.orderDetailModel.orderNo;
    [headView addSubview:orderNoLabel];
    UIButton *copyBtn = [UIButton buttonWithType:UIButtonTypeCustom font:KFontSize(15.0) frame:CGRectMake(headView.width - KScaleWidth(16.0) - KScaleWidth(54.0), orderNoLabel.y + (orderNoLabel.height - KScaleWidth(24.0))/2, KScaleWidth(54.0), KScaleWidth(24.0)) title:@"复制" titleColor:kColorHEX(0x666666)];
    [copyBtn setRadius:copyBtn.height/2 borderColor:kColorHEX(0xCCCCCC) borderWidth:1];
    [copyBtn addTarget:self action:@selector(copyAction:) forControlEvents:UIControlEventTouchUpInside];
    [headView addSubview:copyBtn];
    
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return KScaleWidth(30.0);
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellName = @"XMOrderMeetingCellName";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellName];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellName];
        cell.textLabel.font = KFontSize(15.0);
        cell.textLabel.textColor = kColorHEX(0x666666);
        cell.detailTextLabel.font = KFontSize(15.0);
        cell.detailTextLabel.textColor = kColorHEX(0x666666);
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    cell.textLabel.text = _titleArr[indexPath.row];
    if (self.detailDataArr.count > indexPath.row) {
        cell.detailTextLabel.text = self.detailDataArr[indexPath.row];
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

#pragma mark - Init
- (UITableView *)tbl {
    if (!_tbl) {
        _tbl = [[UITableView alloc] initWithFrame:CGRectMake(0, kNavagationBarH + KScaleWidth(30.0), self.width, self.height - KScaleWidth(30.0) - kNavagationBarH) style:UITableViewStylePlain];
        _tbl.delegate = self;
        _tbl.dataSource = self;
        _tbl.backgroundColor = kColorHEX(0xF6F6F6);
        _tbl.separatorStyle = UITableViewCellSeparatorStyleNone;
//        [_tbl registerClass:<#(nullable Class)#> forCellReuseIdentifier:<#(nonnull NSString *)#>];
        if (@available(iOS 15.0, *)) {
            _tbl.sectionHeaderTopPadding = 0;
        } else {
            _tbl.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            // Fallback on earlier versions
        }
    }
    return _tbl;
}

- (UILabel *)addressLabel {
    if (!_addressLabel) {
        _addressLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(23.0), 0, KScaleWidth(256.0) - KScaleWidth(23.0), KScaleWidth(18.0)) font:KFontSize(13.0) textColor:kColorHEX(0x666666)];
    }
    return _addressLabel;
}

- (UILabel *)phoneLabel {
    if (!_phoneLabel) {
        _phoneLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.addressLabel.x, _addressLabel.maxY + KScaleWidth(30.0), _addressLabel.width, KScaleWidth(20.0)) font:KFontSize(14.0) textColor:kColorHEX(0x666666)];
    }
    return _phoneLabel;
}

- (UIImageView *)avatarImgView {
    if (!_avatarImgView) {
        _avatarImgView = [[UIImageView alloc] initWithFrame:CGRectMake(KScaleWidth(16.0), KScaleWidth(16.0), KScaleWidth(36.0), KScaleWidth(36.0))];
        [_avatarImgView setCornerRadius:_avatarImgView.height/2];
    }
    return _avatarImgView;
}

- (UILabel *)nameLabel {
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.avatarImgView.maxX + KScaleWidth(8.0), _avatarImgView.y, self.width - _avatarImgView.maxX - KScaleWidth(8.0) - KScaleWidth(16.0), _avatarImgView.height) font:KFontSize(18.0) textColor:kColorHEX(0x333333)];
    }
    return _nameLabel;
}

- (XMConfirmOrderMeetCell *)meetInfoView {
    if (!_meetInfoView) {
        _meetInfoView = [[XMConfirmOrderMeetCell alloc] initWithFrame:CGRectMake(0, KScaleWidth(58.0), self.width, KScaleWidth(100.0))];
    }
    return _meetInfoView;
}

- (UILabel *)payPriceLabel {
    if (!_payPriceLabel) {
        _payPriceLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.width/2, KScaleWidth(164.0), self.width/2 - KScaleWidth(16.0), KScaleWidth(25.0))];
        _payPriceLabel.textAlignment = NSTextAlignmentRight;
    }
    return _payPriceLabel;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
